package com.markerhub.controller;


import com.markerhub.common.lang.Result;
import com.markerhub.entity.Course;
import com.markerhub.entity.CourseLearnUnit;
import com.markerhub.service.CourseLearnUnitService;
import com.markerhub.service.CourseService;
import com.markerhub.service.TeachingStudentLearningService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/teaching-student-learning")
public class TeachingStudentLearningController {
    @Autowired
    private TeachingStudentLearningService learningService;

    @Autowired
    private CourseLearnUnitService unitService;

    @Autowired
    private CourseService courseService;

    @GetMapping("/progress/{unitId}")
    public Result getUnitProgress(@PathVariable Integer unitId) {
        try {
            // 获取单元信息
            CourseLearnUnit unit = unitService.getById(unitId);
            if (unit == null) {
                return Result.fail("单元不存在");
            }

            // 获取课程信息
            Course course = courseService.getById(unit.getCourseId());
            if (course == null) {
                return Result.fail("课程不存在");
            }

            // 获取学生完成情况
            List<Map<String, Object>> progressList = learningService.getStudentProgress(unitId, course.getClassId());
            return Result.succ(progressList);

        } catch (Exception e) {
            log.error("获取单元完成情况失败", e);
            return Result.fail("获取单元完成情况失败");
        }
    }
}
